1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 module hip.console.log;
12 import hip.console.console;
13 import hip.util.conv:to;
14 import hip.util.format;
15 
16 private string[] logHistory = [];
17 
18 
19 ///Creates a variable which is always logged whenever modified.
20 struct Logged(T)
21 {
22     T val;
23     private string name;
24     this(T initial, string name = "")
25     {
26         this.val = initial;
27         this.name = name;
28     }
29 
30     pragma(inline, true) void print(T value, string f = __FILE__, size_t l = __LINE__)
31     {
32         rawlog("Modified ", name, " from ", val, " to ", value, " at ", f, ":", l);
33     }
34 
35     auto opAssign(T value, string f = __FILE__, size_t l = __LINE__)
36     {
37         print(value, f, l);
38         val = value;
39         return this;
40     }
41     auto opOpAssign(string op, T)(T value, string f = __FILE__, size_t l = __LINE__)
42     {
43         T newV = mixin("val",op,"value");
44         print(newV, f, l);
45         val = newV;
46         return this;
47     }
48     bool opEquals(const T other) const{return val == other;}
49     auto opUnary(string op)(string f = __FILE__, size_t l = __LINE__)
50     {
51         static if(op == "++" || op == "--")
52         {
53             T oldV = val;
54             mixin(op,"val;");
55             rawlog("Modified ", name, " from ", oldV, " to ", val, " at ", f, ":", l);
56             return val;
57         }
58         else
59             return mixin(op,"val;");
60     }
61 }
62 private string _formatPrettyFunction(string f)
63 {
64     import hip.util.string : lastIndexOf;
65 
66     return f[0..f.lastIndexOf("(")];
67 }
68 
69 
70 /**
71 *   hiplog is a special function and should be used only within the engine for documentation.
72 *   It generates less data by not taking the function name and simplifying the read load.
73 *   Think of that as a verbose of what the engine is currently doing.
74 */
75 void hiplog(Args...)(Args a, string file = __FILE__,
76 string func = __PRETTY_FUNCTION__,
77 ulong line = __LINE__)
78 {
79     import hip.config.opts;
80     static if(HIP_TRACK_HIPLOG)
81         Console.DEFAULT.hipLog("HIP: ", a, " [[", file, ":", line, "]]");
82     else
83         Console.DEFAULT.hipLog("HIP: ", a);
84 }
85 
86 
87 void logln(Args...)(Args a, string file = __FILE__,
88 string func = __PRETTY_FUNCTION__,
89 ulong line = __LINE__)
90 {
91     Console.DEFAULT.log(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction);
92 }
93 
94 
95 mixin template loglnVars(Args...)
96 {
97     enum log = ()
98     {
99         import hip.console.console;
100         static foreach(i; 0..Args.length)
101             Console.DEFAULT.log(__traits(identifier, Args[i]),": ", Args[i]);
102     };
103 }
104 
105 void loglnInfo(Args...)(Args a, string file = __FILE__,
106 string func = __PRETTY_FUNCTION__,
107 ulong line = __LINE__)
108 {
109     Console.DEFAULT.info(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction);
110 }
111 
112 
113 void loglnWarn(Args...)(Args a, string file = __FILE__,
114 string func = __PRETTY_FUNCTION__,
115 ulong line = __LINE__)
116 {
117     Console.DEFAULT.warn(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction);
118 }
119 
120 
121 void loglnError(Args...)(Args a, string file = __FILE__,
122 string func = __PRETTY_FUNCTION__,
123 ulong line = __LINE__)
124 {
125     Console.DEFAULT.error(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction);
126 }
127 
128 
129 void loglnImpl(string s, string f = __FILE__, string fn = __PRETTY_FUNCTION__, ulong l = __LINE__){logln(s,f,fn,l);}
130 void loglnInfoImpl(string s, string f = __FILE__, string fn = __PRETTY_FUNCTION__, ulong l = __LINE__){loglnInfo(s,f,fn,l);}
131 void loglnWarnImpl(string s, string f = __FILE__, string fn = __PRETTY_FUNCTION__, ulong l = __LINE__){loglnWarnImpl(s,f,fn,l);}
132 void loglnErrorImpl(string s, string f = __FILE__, string fn = __PRETTY_FUNCTION__, ulong l = __LINE__){loglnErrorImpl(s,f,fn,l);}
133 void rawlogImpl(string str){Console.DEFAULT.log(str);}
134 void rawwarnImpl(string str){Console.DEFAULT.warn(str);}
135 void rawinfoImpl(string str){Console.DEFAULT.info(str);}
136 void rawerrorImpl(string str){Console.DEFAULT.error(str);}
137 void rawfatalImpl(string str){Console.DEFAULT.fatal(str);}
138 
139 void rawlog(Args... )(Args a){Console.DEFAULT.log(a);}
140 void rawwarn(Args... )(Args a){Console.DEFAULT.warn(a);}
141 void rawinfo(Args... )(Args a){Console.DEFAULT.info(a);}
142 void rawerror(Args... )(Args a){Console.DEFAULT.error(a);}
143 void rawfatal(Args... )(Args a){Console.DEFAULT.fatal(a);}